package day21;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * ArrayStreamDemo.java
 * @author wangbiao<br/>
 *         email:785497344@qq.com
 * @description 直接操作字节数组的流对象，源和目的地都是内存
 * 				CharArrayReader和CharArrayWriter
 * 				StringReader和StringWriter
 * @since 1.0.0
 * @date 2013-6-30下午9:38:32
 *
 */
public class ArrayStreamDemo {

	public static void main(String[] args) {
		ByteArrayInputStream bais = new ByteArrayInputStream(
				"Wangbiao".getBytes());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		int len = 0;
		while ((len = bais.read()) != -1) {
			baos.write(len);
		}
		
		System.out.println("String="+baos.toString());
		System.out.println("size="+baos.size());
		
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("byte.txt");
			//把字节数组中的内容写入到一个流对象中
			baos.writeTo(fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
